package com.century.sjt.activity.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.fragment.AlreadyPayFragment;
import com.century.sjt.fragment.CompleteFragment;
import com.century.sjt.fragment.NotPayFragment;
import com.century.sjt.fragment.StayTakeGoodsFragment;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:周玲波
 * @Description: 用户:我的订单
 * @date : 2016/8/20 12:36
 */
public class MyOrderActivity extends BaseActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{

    private SjtTitleBar title;
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;
    private NotPayFragment tab1;
    private AlreadyPayFragment tab2;
    private StayTakeGoodsFragment tab3;
    private CompleteFragment tab4;
    private LinearLayout mTab1, mTab2, mTab3, mTab4;
    private TextView mTV1, mTV2, mTV3, mTV4;
    private ImageView mTabline;
    private int mScreen1_4;//1/4屏幕宽
    private int mCurrentPageIndex;//标记


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        initView();
        getScreenSize();
        initData();
        initEvent();
    }

    /**
     * @Description: 初始化
     * @author: 周玲波
     * @date: 2016/9/10 14:45
     */
    @Override
    protected void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        title = (SjtTitleBar) findViewById(R.id.my_order_title);
        mDatas = new ArrayList<Fragment>();
        tab1 = new NotPayFragment();
        tab2 = new AlreadyPayFragment();
        tab3 = new StayTakeGoodsFragment();
        tab4 = new CompleteFragment();
        mTab1 = (LinearLayout) findViewById(R.id.ll_not_pay);
        mTab2 = (LinearLayout) findViewById(R.id.ll_already_pay);
        mTab3 = (LinearLayout) findViewById(R.id.ll_stay_goods);
        mTab4 = (LinearLayout) findViewById(R.id.ll_complete);
        mTabline = (ImageView) findViewById(R.id.id_iv_tabline);
        mTV1 = (TextView) findViewById(R.id.tv_not_pay);
        mTV2 = (TextView) findViewById(R.id.tv_already_pay);
        mTV3 = (TextView) findViewById(R.id.tv_stay_goods);
        mTV4 = (TextView) findViewById(R.id.tv_complet);

    }

    /**
     * @Description: 监听
     * @author: 周玲波
     * @date: 2016/9/10 14:48
     */
    @Override
    protected void initEvent() {
        title.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTab1.setOnClickListener(this);
        mTab2.setOnClickListener(this);
        mTab3.setOnClickListener(this);
        mTab4.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(this);

    }

    /**
     * @Description: 初始化数据
     * @author: 周玲波
     * @date: 2016/9/10 14:49
     */
    @Override
    protected void initData() {
        title.setTitle(getResources().getString(R.string.text_order));
        title.setLeftIconResource(getResources().getString(R.string.icon_return));
        mDatas.add(tab1);
        mDatas.add(tab2);
        mDatas.add(tab3);
        mDatas.add(tab4);
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mDatas.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mDatas.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);
    }

    /**
     * @Description: 获取屏大小
     * @author: 周玲波
     * @date: 2016/9/10 15:10
     */
    private void getScreenSize() {
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_4 = outMetrics.widthPixels / 4;
        ViewGroup.LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_4;
        mTabline.setLayoutParams(lp);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            //待支付
            case R.id.ll_not_pay:
                resetTextView();
                mViewPager.setCurrentItem(0);
                break;
            //已支付
            case R.id.ll_already_pay:
                resetTextView();
                mViewPager.setCurrentItem(1);
                break;
            //待收货
            case R.id.ll_stay_goods:
                resetTextView();
                mViewPager.setCurrentItem(2);
                break;
            //已完成
            case R.id.ll_complete:
                resetTextView();
                mViewPager.setCurrentItem(3);
                break;
        }
    }
    /**
     * @Description: 设置TV字体颜色
     * @author: 周玲波
     * @date: 2016/9/10 15:24
     */
    protected void resetTextView() {
        mTV1.setTextColor(getResources().getColor(R.color.textdefault_title));
        mTV2.setTextColor(getResources().getColor(R.color.textdefault_title));
        mTV3.setTextColor(getResources().getColor(R.color.textdefault_title));
        mTV4.setTextColor(getResources().getColor(R.color.textdefault_title));
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline
                .getLayoutParams();
        if (mCurrentPageIndex == 0 && position == 0)// 0->1
        {
            lp.leftMargin = (int) (positionOffset * mScreen1_4+ mCurrentPageIndex
                    * mScreen1_4);
        } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
        {
            lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4+ (positionOffset - 1)
                    * mScreen1_4);
        } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
        {
            lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4+ positionOffset
                    * mScreen1_4);
        } else if (mCurrentPageIndex == 2 && position == 1) // 2->1
        {
            lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + ( positionOffset-1)
                    * mScreen1_4);
        }else if(mCurrentPageIndex == 2 && position == 2)//2->3
        {
            lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4+ positionOffset
                    * mScreen1_4);
        }else if(mCurrentPageIndex == 3 && position == 2)//3->2
        {
            lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + ( positionOffset-1)
                    * mScreen1_4);
        }
        mTabline.setLayoutParams(lp);
    }

    @Override
    public void onPageSelected(int position) {
        resetTextView();
        switch (position) {
            case 0:
                mTV1.setTextColor(getResources().getColor(R.color.textdefault_title));
                break;
            case 1:
                mTV2.setTextColor(getResources().getColor(R.color.textdefault_title));
                break;
            case 2:
                mTV3.setTextColor(getResources().getColor(R.color.textdefault_title));
                break;
            case 3:
                mTV4.setTextColor(getResources().getColor(R.color.textdefault_title));
                break;
        }
        mCurrentPageIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
